/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getCovereds;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getFragments;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getOperands;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionOperand;
import org.eclipse.uml2.uml.Lifeline;

/**
 * Command to combine two InteractionOperands into one, removing the second.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>InteractionOperand - the operand <i>below</i> the separator. This
 * operand will be joined with the one in front of it.
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class UnionInteractionOperandsCommand extends Command {

	private CombinedFragment cfragment;
	private InteractionOperand upperOperand;
	private InteractionOperand lowerOperand;
	private Map<Lifeline, LifelinePosition> oldPositions;

	public void setInteractionOperand(final InteractionOperand operand) {
		this.lowerOperand = operand;
	}

	@Override
	public void execute() {
		recordCurrentState();

		upperOperand = getUpperOperand();
		getFragments(upperOperand).addAll(getFragments(lowerOperand));

		cfragment.getOperands().remove(lowerOperand);
	}

	@Override
	public void undo() {
		final SplitInteractionOperandCommand cmd = new SplitInteractionOperandCommand();
		cmd.setInteractionOperand(upperOperand);
		cmd.setLowerOperand(lowerOperand);
		cmd.setPositions(oldPositions.values());
		try {
			cmd.execute();
		} finally {
			cmd.dispose();
		}

		oldPositions.clear();
		oldPositions = null;
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void dispose() {
		cfragment = null;
		lowerOperand = null;
		upperOperand = null;
		if (oldPositions != null) { // will be null if cmd was undone
			oldPositions.clear();
			oldPositions = null;
		}
	}

	private void recordCurrentState() {
		cfragment = (CombinedFragment) lowerOperand.getOwner();

		oldPositions = new HashMap<Lifeline, LifelinePosition>();
		for (InteractionFragment f : getFragments(lowerOperand)) {
			for (Lifeline lifeline : getCovereds(f))
				if (!oldPositions.containsKey(lifeline)) {
					int index = lifeline.getCoveredBys().indexOf(f);
					oldPositions.put(lifeline, new LifelinePosition(lifeline,
							index));
				}
		}

		// for (Lifeline lifeline : getCovereds(cfragment)) {
		// if (!oldPositions.containsKey(lifeline))
		// oldPositions.put(lifeline, new LifelinePosition(lifeline, -1));
		// }
	}

	private InteractionOperand getUpperOperand() {
		List<InteractionOperand> operands = getOperands(cfragment);
		// There should always be an operand before since it is the
		// *lower* operand that is merged into its predecessor
		return operands.get(operands.indexOf(lowerOperand) - 1);
	}
}
